package com.zhuss.product.processor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zhuss.enums.SignStatus;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.product.line.SearchLineBo;
import com.zhuss.bean.bo.product.preline.SearchPrelineBo;
import com.zhuss.bean.bo.product.preline.SearchPrelineSignBo;
import com.zhuss.bean.vo.product.preline.DetailPrelineSignVo;
import com.zhuss.bean.vo.product.preline.DetailPrelineVo;
import com.zhuss.bean.vo.product.preline.ListPrelineSignVo;
import com.zhuss.bean.vo.product.prelinesite.ListPrelineSiteVo;
import com.zhuss.common.ListVo;
import com.zhuss.common.PageVo;
import com.zhuss.exception.DzcxException;
import com.zhuss.product.db.dao.ext.PrelineDao;
import com.zhuss.product.db.dao.ext.PrelineSignDao;
import com.zhuss.product.db.dao.ext.PrelineSiteDao;
import com.zhuss.product.db.dao.ext.SiteDao;
import com.zhuss.product.db.entity.PrelineSign;


/**
 * @description: 招募报名读
 * @author: zhushanshuo
 * @create: Wed Jan 06 16:26:20 CST 2021
 **/
@Component("prelineSignRead")
public class PrelineSignRead {

	@Autowired
	private PrelineSignDao prelineSignDao;
	@Autowired
	private PrelineSiteDao prelinesiteDao;
	@Autowired
	private SiteDao siteDao;
	@Autowired
	private PrelineDao prelineDao;
	public Map<String, PrelineSign> listMap() {
		List<PrelineSign> list = prelineSignDao.listAll();
		Map<String, PrelineSign> map = new HashMap<>();
		for (PrelineSign c : list) {
			map.put(c.getName(), c);
		}
		return map;
	}

	/**
	 * 招募报名
	 * @return
	 */
	public Object listPrelineSign() {
		List<PrelineSign> prelineSignList = prelineSignDao.listAll();
		List<ListVo> result = new ArrayList<>();
		transferList(prelineSignList, result);
		return result;
	}


	/**
	 * 招募报名
	 * @return
	 */
	public Object listPrelineSign(List<Long> prelineSignIdList) {
		List<PrelineSign> result = prelineSignDao.listById(prelineSignIdList);
		return result;
	}


	/**
	 * 根据名字查询招募报名
	 * @param
	 * @return
	 */
	public List<PrelineSign> list(String name) {
		List<PrelineSign> result = prelineSignDao.listByName(name);
		return result;
	}

	public List<PrelineSign> listNotId(String name, Long prelineSignId) {
		List<PrelineSign> result = new ArrayList<PrelineSign>();
		List<PrelineSign> list = list(name);
		for (PrelineSign c : list) {
			if (!c.getPrelineSignId().equals(prelineSignId)) {
				result.add(c);
				break;
			}
		}
		return result;
	}

	/**
	 * 招募报名
	 * @return
	 */
	public Object listPrelineSignId() {
		List<PrelineSign> prelineSignList = prelineSignDao.listAll();
		List<Long> result = new ArrayList<>();
		transferListId(prelineSignList, result);
		return result;
	}

	private void transferListId(List<PrelineSign> prelineSignList, List<Long> result) {
		for (PrelineSign item : prelineSignList) {
			result.add(item.getPrelineSignId());
		}
	}

	private void transferList(List<PrelineSign> prelineSignList, List<ListVo> result) {
		for (PrelineSign item : prelineSignList) {
			ListVo vo = new ListVo();
			vo.setKey(item.getPrelineSignId());
			vo.setLabel(item.getName());
			result.add(vo);
		}

	}

	/**
	 * 招募报名列表
	 * @param bo
	 * @return
	 */
	public Object list(SearchPrelineSignBo bo) {
		PageVo<ListPrelineSignVo> result = new PageVo<ListPrelineSignVo>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
        result.setItems(prelineSignDao.list(bo));
        result.setTotal(prelineSignDao.countList(bo));
		return result;
	}

	/**
	 * 招募报名详情
	 * @param prelineId
	 * @return
	 * @throws DzcxException
	 */
	public Object detail(Long prelineId) throws DzcxException {

		List<ListPrelineSiteVo> res = prelinesiteDao.listByprelineId(prelineId);
		DetailPrelineVo vo = prelineDao.detail(prelineId);
		if(vo!=null) {
			if (res == null) {
//			throw new DzcxException(ExceptionCode.数据不存在);
				return vo;
			} else {
				res.forEach(siteVo -> {
					siteVo.setName(siteDao.selectByPrimaryKey(siteVo.getSiteId()).getName());
				});
				vo.setPrelsiteList(res);
				return vo;
			}
		}else{
			return null;
		}

	}


	public int isApply(SearchPrelineBo bo) {
		return prelineSignDao.countApply(bo);
	}

	public DetailPrelineSignVo getDetail(DetailPrelineSignVo bean) {
		return prelineSignDao.getDetail(bean);
	}


	public List<ListPrelineSiteVo> listByprelineId(Long prelineId) {
		List<ListPrelineSiteVo> res = prelinesiteDao.listByprelineId(prelineId);

		if (res == null) {
//			throw new DzcxException(ExceptionCode.数据不存在);
			return null;
		} else {
			res.forEach(siteVo -> {
				siteVo.setName(siteDao.selectByPrimaryKey(siteVo.getSiteId()).getName());
			});
			return res;
		}
	}

	/**
	 * 我的报名
	 * @param bo
	 * @return
	 * @throws DzcxException
	 */
	public Object mySignList(SearchPrelineSignBo bo) throws DzcxException{
		PageVo<ListPrelineSignVo> result = new PageVo<ListPrelineSignVo>();
		result.setPageNum(bo.getPageNum());
		result.setPageSize(bo.getPageSize());
		bo.setStatus(SignStatus.sign.key());
		List<ListPrelineSignVo> list = prelineSignDao.list(bo);
		if (list.size() > 0) {
			for (ListPrelineSignVo sign : list) {
				//查询linepre主表
				DetailPrelineVo vo = prelineDao.detail(sign.getPrelineId());
				sign.setStartPointName(vo.getStartPointName());
				sign.setEndPointName(vo.getEndPointName());
				sign.setApplyCount(prelineSignDao.countByPrelineId(sign.getPrelineId()));
				List<ListPrelineSiteVo> res = prelinesiteDao.listByprelineId(sign.getPrelineId());
				for (ListPrelineSiteVo siteVo : res) {
					siteVo.setName(siteDao.selectByPrimaryKey(siteVo.getSiteId()).getName());
				}
				sign.setLinesiteList(res);
			}
		}
		result.setItems(list);
		result.setTotal(prelineSignDao.countList(bo));
		return result;
	}

	public List<Long> listByUser(SearchLineBo bo) {
		List<Long> lineIdList = prelineSignDao.listByUser(bo);
		return lineIdList;
	}
}
